﻿Imports System
Imports System.Collections
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.XPath
Imports unRar

Public Class ReadingList
    Implements IDisposable

    Private _readingList As List(Of String)
    Private _curFile As String
    Private _curIndex As Integer = 0 
    Private path As String
    Private TempFolder As String



    Public Property CurrentFile As String
    Public ReadOnly Property FileList As List(Of String)
        Get
            Return _readingList
        End Get
    End Property

    Public Sub New(ByVal currentFile As String, ByVal lst As List(Of String))
        _readingList = lst
        Me.CurrentFile = currentFile
        Me._curIndex = _readingList.FindIndex(AddressOf findIndexFunction)
    End Sub
    Public Sub New(ByVal currentFile As String, ByVal arr As Array)
        _readingList = New List(Of String)
        _readingList.AddRange(arr)
        Me.CurrentFile = currentFile
        Me._curIndex = _readingList.FindIndex(AddressOf findIndexFunction)
    End Sub
    Public Sub New(ByVal currentFile As String)
        Me.New(currentFile, IO.Directory.GetFiles(IO.Path.GetDirectoryName(currentFile)))
    End Sub


    Private Function findIndexFunction(ByVal fn As String) As Boolean
        If String.Compare(fn, CurrentFile) = 0 Then
            Return True
        Else
            Return False
        End If
    End Function




    Public Function getFileByIndex(ByVal index As Integer) As String
        If index >= 0 AndAlso index <= Me._readingList.Count Then
            Me._curFile = _readingList(index)
            Me._curIndex = index
        Else
            Throw New ArgumentOutOfRangeException("Index")
        End If
        Return Me._curFile
    End Function

    Public Function GetNextFile() As String
        _curIndex += 1
        If _curIndex >= Me._readingList.Count Then _curIndex = Me._readingList.Count - 1
        _curFile = Me._readingList(_curIndex)
        Return _curFile
    End Function

    Public Function HasPrevious() As Boolean
        If _curIndex > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function HasNext() As Boolean
        If _curIndex < Me._readingList.Count Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetPreviousFile() As String
        _curIndex -= 1
        If _curIndex <= 0 Then _curIndex = 0
        _curFile = Me._readingList(_curIndex)
        Return _curFile
    End Function




#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
